<?php

namespace app\admin\controller;

use think\Controller;
use app\admin\model\Admin;
use think\Request;

class LoginController extends Controller {
    /*
     * 登录页面
     */

    public function index() {
        return view('index');
    }

    /*
     * 登陆
     */

    public function login() {
        $admin_name = input('post.admin_name');
        $admin_pwd = input('post.admin_pwd');
        $login_ip = Request::instance()->ip();
//        if(($admin_name != 'administrator' && $admin_name != 'hyz') && ($login_ip != '123.115.116.232' && $login_ip != '123.115.114.194')){ 
//            $array = [
//                'code' => 3,
//                'msg' => '登录环境异常，请到指定电脑登录',
//                'data' => null,
//            ];
//            return json_encode($array);
//        }

        $admin = new Admin();

        $row = $admin->where('admin_name', '=', $admin_name)
                ->where('admin_pwd', '=', md5($admin_pwd))
                ->where('status', '=', Admin::STATUS_1)
                ->field('id,admin_name,admin_note,photo,role,jobnum,type')
                ->find();

        if ($row) {
//          Session::set('name', 'thinkphp');
            session(config('adminconfig.USER_AUTH_KEY'), $row['id']);
            session("AI_NAME", $row['admin_note']);
            session("AI_ID", $row['id']);
            session("AI_PHOTO", $row['photo']);
            session("AI_ROLE", $row['role']);
            $token = md5(md5($admin_name . time()) . $admin_pwd . time());
            $data = [
                "last_time" => date('Y-m-d H:i:s'),
                "login_ip" => Request::instance()->ip(),
                'token' => $token,
                "valid_time" => date('Y-m-d H:i:s',  time()+3600*24),
            ];

            $admin->where('id', '=', $row['id'])->update($data);

            if ($row['admin_name'] == config('adminconfig.RBAC_SUPERADMIN')) {
                session(config('adminconfig.ADMIN_AUTH_KEY'), true);
            }
            $row['token'] = $token;
            $array = [
                'code' => 0,
                'msg' => '登录成功',
                'data' => $row,
            ];
            //header('Access-Control-Allow-Origin: *');
            header("Access-Control-Allow-Headers: Origin, X-Requested-With, Content-Type, Accept,token");
            header('Access-Control-Allow-Methods: GET, POST, PUT');
            header('Content-Type:application/json; charset=utf-8');
            return json_encode($array);
        } else {
            $array = [
                'code' => 3,
                'msg' => '登录失败',
                'data' => $row,
            ];
            //header('Access-Control-Allow-Origin: *');
            header("Access-Control-Allow-Headers: Origin, X-Requested-With, Content-Type, Accept,token");
            header('Access-Control-Allow-Methods: GET, POST, PUT');
            header('Content-Type:application/json; charset=utf-8');
            return json_encode($array);
        }
    }

    /*
     * 退出登录
     * author xfwed
     * date 2017.08.02
     */

    public function logout() {
        $id = input('param.id');
        $data = [
            'token' => '',
        ];
        Admin::where('id', '=', $id)->update($data);
        $array = [
            'code' => 0,
            'msg' => '成功退出',
            'data' => null,
        ];
        //header('Access-Control-Allow-Origin: *');
        header("Access-Control-Allow-Headers: Origin, X-Requested-With, Content-Type, Accept,token");
        header('Access-Control-Allow-Methods: GET, POST, PUT');
        header('Content-Type:application/json; charset=utf-8');
        return json_encode($array);
    }

}
